﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Service.Interface;
using Domain;
using Clannan.Models.JsonModel;

namespace Clannan.Controllers
{
    public class MemoController : Controller
    {
        public IMemoService MemoService;

        /// <summary>
        /// 获取备忘录分页集合
        /// </summary>
        /// <param name="pageIndex">页索引</param>
        /// <param name="pageSize">页大小</param>
        /// <returns></returns>
        public JsonResult GetByPage(string pageIndex, string pageSize)
        {
            List<MemoJsonModel> memoList = new List<MemoJsonModel>();//Json返回页集合
            MemoJsonModel memoJsonModel; //Json模型
            int pageCount;               //总页数

            //获取分页数据
            IList<Memo> memoListData = this.MemoService
                .LoadAllMemoByPage(Convert.ToInt32(User.Identity.Name), Convert.ToInt32(pageIndex), Convert.ToInt32(pageSize), out pageCount, false);
            //数据处理
            foreach (Memo memo in memoListData)
            {
                memoJsonModel = new MemoJsonModel();
                memoJsonModel = memoJsonModel.Copy(memoJsonModel, memo);
                memoList.Add(memoJsonModel);
            }

            return Json(new { Message = 1, MemoList = memoList, PageCount = pageCount });
        }

        /// <summary>
        /// 获取备忘录
        /// </summary>
        /// <param name="memoID">备忘录ID</param>
        /// <returns></returns>
        public JsonResult Get(string memoID)
        {
            Memo memo = this.MemoService.Get(memoID);
            var result = new
            {
                ID = memo.ID,
                Content = memo.Content,
                CreateTime = memo.CreateTime,
                WarnTime = memo.WarnTime
            };

            return Json(result);
        }

        /// <summary>
        /// 创建备忘录
        /// </summary>
        /// <param name="content">提醒内容</param>
        /// <param name="warnTime">提醒时间</param>
        /// <returns></returns>
        public JsonResult Create(string content, string warnTime)
        {
            Memo memo = new Memo();
            memo.Content = content;
            memo.CreateTime = DateTime.Now;
            memo.WarnTime = Convert.ToDateTime(warnTime);
            memo.MarkDelete = false;
            memo.UserInfo = new UserInfo { ID = Convert.ToInt32(User.Identity.Name) };
            this.MemoService.Save(memo);

            return Json(new { Message = 1 });
        }

        /// <summary>
        /// 修改备忘录
        /// </summary>
        /// <param name="memoID">备忘录ID</param>
        /// <param name="content">提醒内容</param>
        /// <param name="warnTime">提醒时间</param>
        /// <returns></returns>
        public JsonResult Update(string memoID, string content, string warnTime)
        {
            Memo memo = this.MemoService.Get(memoID);
            memo.Content = content;
            memo.WarnTime = Convert.ToDateTime(warnTime);
            this.MemoService.Save(memo);

            return Json(new { Message = 1 });
        }

        /// <summary>
        /// 将备忘录放入垃圾箱
        /// </summary>
        /// <param name="memoID">备忘录ID</param>
        /// <returns></returns>
        public JsonResult Throw(string memoID)
        {
            this.MemoService.UpdateMemoMarkDelete(memoID, true);

            return Json(new { Message = 1 });
        }

        /// <summary>
        /// 从垃圾箱中还原备忘录
        /// </summary>
        /// <param name="memoID">备忘录ID</param>
        /// <returns></returns>
        public JsonResult Restore(string memoID)
        {
            this.MemoService.UpdateMemoMarkDelete(memoID, false);

            return Json(new { Message = 1 });
        }

        /// <summary>
        /// 删除备忘录
        /// </summary>
        /// <param name="memoID">备忘录ID</param>
        /// <returns></returns>
        public JsonResult Delete(string memoID)
        {
            this.MemoService.Delete(memoID);

            return Json(new { Message = 1 });
        }

    }
}
